const router = require("express").Router();
const fs = require("fs");
let listsData = JSON.parse(fs.readFileSync("./data/lists.json").toString());

if (!listsData.data) {
    listsData.data = [];
}


router.get("/", (req, res) => {
    res.render("./list/list", listsData);
})


router.get("/new", (req, res) => {
    res.render("./list/new", {});
})


router.post("/", (req, res) => {
    listsData.data.push(req.body);
    fs.writeFileSync("./data/lists.json", JSON.stringify(listsData));
    res.redirect("/lists");
})


module.exports = router;